package com.avic.modules.statistics.service.impl;

import com.avic.common.service.impl.BaseServiceImpl;
import com.avic.common.utils.QueryHelpPlus;
import com.avic.modules.equipment.service.mapper.EquipmentMapper;
import com.avic.modules.statistics.domain.MonthlyStatusRecord;
import com.avic.modules.statistics.service.MonthlyStatusRecordService;
import com.avic.modules.statistics.service.dto.MonthlyStatusRecordQueryCriteria;
import com.avic.modules.statistics.service.mapper.MonthlyStatusRecordMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@Service(value = "monthlyStatusRecordService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MonthlyStatusRecordServiceImpl extends BaseServiceImpl
        <MonthlyStatusRecordMapper, MonthlyStatusRecord> implements MonthlyStatusRecordService{

    private final MonthlyStatusRecordMapper monthlyStatusRecordMapper;
    private final EquipmentMapper equipmentMapper;

    public MonthlyStatusRecordServiceImpl(MonthlyStatusRecordMapper monthlyStatusRecordMapper, EquipmentMapper equipmentMapper) {
        this.monthlyStatusRecordMapper = monthlyStatusRecordMapper;
        this.equipmentMapper = equipmentMapper;
    }

    /**
     * 清空表数据
     */
    @Override
    @Scheduled(cron = "0 0 1 1 * ?")
    public void del() {
        monthlyStatusRecordMapper.del();
    }

    @Override
    public void insert(MonthlyStatusRecord record) {
        monthlyStatusRecordMapper.insert(record);
    }

    /**
     * 根据设备id查看数据
     * @param equipId
     * @return
     */
    @Override
    public List<MonthlyStatusRecord> queryMonthlyStatusRecord(String equipId) {
        return monthlyStatusRecordMapper.selectMonthlyStatistics(equipId);
    }

    @Override
    public List<MonthlyStatusRecord> queryMonthlyStatusRecordTotal(String equipId, String creationDate) {
        MonthlyStatusRecordQueryCriteria criteria = new MonthlyStatusRecordQueryCriteria();
        criteria.setCreationDate(creationDate);
        criteria.setEquipId(equipId);
        return baseMapper.selectList(QueryHelpPlus.getPredicate(MonthlyStatusRecord.class,criteria));
    }
}
